namespace PickGold.Charting
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;

    internal class FontCache : IDisposable
    {
        private static string _defaultFamilyName;
        private Dictionary<KeyInfo, Font> _fontCache = new Dictionary<KeyInfo, Font>(new KeyInfo.EqualityComparer());

        public void Dispose()
        {
            foreach (Font font in this._fontCache.Values)
            {
                font.Dispose();
            }
            this._fontCache.Clear();
            GC.SuppressFinalize(this);
        }

        public Font GetFont(string familyName, int size)
        {
            KeyInfo key = new KeyInfo(familyName, (float) size);
            if (!this._fontCache.ContainsKey(key))
            {
                this._fontCache.Add(key, new Font(familyName, (float) size));
            }
            return this._fontCache[key];
        }

        public Font GetFont(FontFamily family, float size, FontStyle style)
        {
            KeyInfo key = new KeyInfo(family, size, style);
            if (!this._fontCache.ContainsKey(key))
            {
                this._fontCache.Add(key, new Font(family, size, style));
            }
            return this._fontCache[key];
        }

        public Font GetFont(string familyName, float size, FontStyle style)
        {
            KeyInfo key = new KeyInfo(familyName, size, style);
            if (!this._fontCache.ContainsKey(key))
            {
                this._fontCache.Add(key, new Font(familyName, size, style));
            }
            return this._fontCache[key];
        }

        public Font GetFont(FontFamily family, float size, FontStyle style, GraphicsUnit unit)
        {
            KeyInfo key = new KeyInfo(family, size, style, unit);
            if (!this._fontCache.ContainsKey(key))
            {
                this._fontCache.Add(key, new Font(family, size, style, unit));
            }
            return this._fontCache[key];
        }

        public Font DefaultBoldFont
        {
            get
            {
                return this.GetFont(DefaultFamilyName, 8f, FontStyle.Bold);
            }
        }

        public static string DefaultFamilyName
        {
            get
            {
                if (_defaultFamilyName == null)
                {
                    foreach (FontFamily family in FontFamily.Families)
                    {
                        if (family.Name == "Microsoft Sans Serif")
                        {
                            _defaultFamilyName = family.Name;
                            break;
                        }
                    }
                    if (_defaultFamilyName == null)
                    {
                        _defaultFamilyName = FontFamily.GenericSansSerif.Name;
                    }
                }
                return _defaultFamilyName;
            }
        }

        public Font DefaultFont
        {
            get
            {
                return this.GetFont(DefaultFamilyName, 8);
            }
        }

        private class KeyInfo
        {
            private string _familyName;
            private int _gdiCharSet;
            private float _size;
            private FontStyle _style;
            private GraphicsUnit _unit;

            public KeyInfo(string familyName, float size)
            {
                this._size = 8f;
                this._unit = GraphicsUnit.Point;
                this._gdiCharSet = 1;
                this._familyName = familyName;
                this._size = size;
            }

            public KeyInfo(FontFamily family, float size, FontStyle style)
            {
                this._size = 8f;
                this._unit = GraphicsUnit.Point;
                this._gdiCharSet = 1;
                this._familyName = family.ToString();
                this._size = size;
                this._style = style;
            }

            public KeyInfo(string familyName, float size, FontStyle style)
            {
                this._size = 8f;
                this._unit = GraphicsUnit.Point;
                this._gdiCharSet = 1;
                this._familyName = familyName;
                this._size = size;
                this._style = style;
            }

            public KeyInfo(FontFamily family, float size, FontStyle style, GraphicsUnit unit)
            {
                this._size = 8f;
                this._unit = GraphicsUnit.Point;
                this._gdiCharSet = 1;
                this._familyName = family.ToString();
                this._size = size;
                this._style = style;
                this._unit = unit;
            }

            internal class EqualityComparer : IEqualityComparer<FontCache.KeyInfo>
            {
                public bool Equals(FontCache.KeyInfo x, FontCache.KeyInfo y)
                {
                    return ((((x._size == y._size) && (x._familyName == y._familyName)) && ((x._unit == y._unit) && (x._style == y._style))) && (x._gdiCharSet == y._gdiCharSet));
                }

                public int GetHashCode(FontCache.KeyInfo obj)
                {
                    return (obj._familyName.GetHashCode() ^ obj._size.GetHashCode());
                }
            }
        }
    }
}

